package com.example.springbootgitlab.controller;

import com.alibaba.fastjson.JSON;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

@RestController
public class ReadFileController {

    @RequestMapping("/read")
    public String readFile(){
        ClassPathResource classPathResource = new ClassPathResource("myfile/username.txt");
        List users = new ArrayList<>();
        try {
            InputStream inputStream = classPathResource.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line = "";
            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
                users.add(line);
            }
            inputStream.close();
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("--------------");
        System.out.println(users.size());
        return JSON.toJSONString(users);
    }

}
